import {userService,baseService} from '#/service'
import {mapGetters,mapActions} from 'vuex'
import {Toast} from 'mint-ui';
import AREA from '~common/config/area.js'
import {Upload} from 'we-plugins'
import BASE_URL from '~common/config/base-url'
import IndexFooter from '@/components/base/IndexFooter'
const resSubmit={
  "acount": "",			//网站账户
  "acountLevel": "一星",		//账户等级
  "baitiaoImg": "",			//花呗，白条截图
  "baitiaoStart": "1",				//
  "myacountImg": "",		//我的账户截图
  "mysiteImg": "",			//我的主页截图
  "receiveAddress": "",		//收货详细地址
  "receiveCity": "北京市",		//收货市
  "receiveCountry": "",		//收货区
  "receiveMobile": "",		//收货人手机号
  "receiveProvince": AREA[0].name,	//收货省
  "receiver": "",			//收货人
  "type": ""				//网站平台类型（淘宝，京
}
export default {
  data(){
    return {
      controllers:{},
      submit:Object.assign({},resSubmit),
      baitiao:[{label:'是',value:"1"},{label:'否',value:"2"}],
      area:AREA,
      cityOption:[],
      areaOption:[],
      result:{},
      rules:{
        acount:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'账号不能为空！'
            },
          ] ,
          value:'',
          join:'and'
        },
        receiveMobile:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'手机号不能为空！'
            },
            {
              type:'patter',
              rule:'phone',
              msg:'手机号码格式不对！'
            },
          ] ,
          value:'',
          join:'and'
        },
        receiver:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'收货人不能为空！'
            }
          ] ,
          value:'',
          join:'and'
        },
        receiveAddress:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'详细地址不能为空！'
            }
          ] ,
          value:'',
          join:'and'
        },
        myacountImg:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'请上传我的账户截图！'
            }
          ] ,
          value:'',
          join:'and'
        },
        mysiteImg:{
          rules:[
            {
              type:'inFun',
              rule:'required',
              msg:'请上传我的主页截图！'
            }
          ] ,
          value:'',
          join:'and'
        },
      },
      isEdit:false,
    }
  },
  created(){
    // this.initData()
    // if(this.user.taobaoList.length<1||this.$route.query.isEdit){
    //   this.isEdit=true
    // }
  },
  computed:{
    ...mapGetters(['user'])
  },
  methods:{
    ...mapActions(['saveUserInfo']),
    levelChange($event){
      this.submit.acountLevel=$event.target.value
    },
    provinceChange($event){
      this.submit.receiveProvince=$event.target.value
      for(let i=0;i<AREA.length;i++){
          if(AREA[i].name===$event.target.value){
            this.cityOption=AREA[i].city
            this.submit.receiveCity=this.cityOption[0].name

            break
          }
        }
        for(let i=0;i<this.cityOption.length;i++){
          if(this.cityOption[i].name===this.submit.receiveCity){
            this.areaOption=this.cityOption[i].area
            this.submit.receiveCountry=this.areaOption[0]
            break
          }
        }
    },
    cityChange($event){
      this.submit.receiveCity=$event.target.value
      for(let i=0;i<this.cityOption.length;i++){
          if(this.cityOption[i].name===$event.target.value){
            this.areaOption=this.cityOption[i].area
            this.submit.receiveCountry=this.areaOption[0]
            break
          }
        }
    },
    areaChange($event){
      this.submit.receiveCountry=$event.target.value
    },
    // async upload(arg,key){
    //
    //   let event=arg[0]
    //   let file=event.target.files[0];
    //   let upload=new Upload(file,{
    //     name:'file',
    //     options:{
    //       url:`${BASE_URL}/upload`,
    //     }
    //   })
    //   upload.on('success',(res)=>{
    //     try{
    //       res=JSON.parse(res)
    //     }catch (e) {
    //     }
    //     if(res.code===0){
    //       this.submit[key]=res.data
    //     }else{
    //       this.$notify({
    //         title: '警告',
    //         message: res.message,
    //         type: 'warning'
    //       });
    //     }
    //
    //   })
    //   await upload.send()
    // },
    submitInfo(){
      // console.log(this.rules, this.submit, 999)
      // return;
      this.$nextTick(()=>{
        this.result=this.$valiForm.form(this.rules);
        if(this.result.adopt){
          const data = { ...this.submit }
          if (typeof data.vipBind !== 'number') {
            if (this.$route.query.type == '0') {
              data.vipBind = 0;
            } else {
              data.vipBind = 1;
            }
          }
          userService.acountBind({
            data:this.submit,
            success:(res)=>{
              this.saveUserInfo(res.data);
              this.isEdit=false
              Toast('提交审核成功');
              this.$router.push('/user');
            }
          })
        }else{
          let keys=Object.keys(this.result)
          for(let i=0;i<keys.length;i++){
            if(keys[i]!=='adopt'){
              this.$toast({
                message: this.result[keys[i]].msg,
                position: 'middle',
                duration: 2000
              });
              return
            }
          }
        }
      })
    },
    edit(item){
      this.submit=Object.assign(this.submit,item);
      this.isEdit=true;
    },
    initData() {
      this.isEdit=true
      if (this.$route.query.type == '0') {
        this.submit = {...(this.user.taobaoList[0] || {})}
      } else if (this.$route.query.type == '1') {
        this.submit = {...(this.user.taobaoList[1] || {})}
      } else {
        this.submit = {}
      }
      this.submit.type=this.name
      for(let i=0;i<AREA.length;i++){
        if(AREA[i].name===this.submit.receiveProvince){
          this.cityOption=AREA[i].city
          break
        }
      }
      for(let i=0;i<this.cityOption.length;i++){
        if(this.cityOption[i].name===this.submit.receiveCity){
          this.areaOption=this.cityOption[i].area
          break
        }
      }
    },
    addblank(){
      this.isEdit=true;
      this.submit=Object.assign({},resSubmit)
    },
    showMsg:function(id){
      this.controllers[id]=!this.controllers[id]
    },
  },
  watch:{
    // isEdit(){
    //   this.$nextTick(()=>{
    //     this.initSelect();
    //   })

    // },
    "submit.acount":{
      immediate:true,
      handler(newVal){

        this.rules.acount.value=newVal
      },
    },
    "submit.receiveMobile":{
      immediate:true,
      handler(newVal){
        this.rules.receiveMobile.value=newVal
      },
    },
    "submit.receiver":{
      immediate:true,
      handler(newVal){
        this.rules.receiver.value=newVal
      },
    },
    "submit.receiveAddress":{
      immediate:true,
      handler(newVal){
        this.rules.receiveAddress.value=newVal
      },
    },
    "submit.myacountImg":{
      immediate:true,
      handler(newVal){
        this.rules.myacountImg.value=newVal
      },
    },
    "submit.mysiteImg":{
      immediate:true,
      handler(newVal){
        this.rules.mysiteImg.value=newVal
      },
    },
    "submit.receiveProvince":{
      immediate:true,
      handler(newVal){


        // for(let i=0;i<AREA.length;i++){
        //   if(AREA[i].name===newVal){
        //     this.cityOption=AREA[i].city
        //     this.submit.receiveCity=this.cityOption[0].name

        //     break
        //   }
        // }
      },
    },
    "submit.receiveCity":{
      immediate:true,
      handler(newVal){

        // for(let i=0;i<this.cityOption.length;i++){
        //   if(this.cityOption[i].name===newVal){
        //     this.areaOption=this.cityOption[i].area
        //     this.submit.receiveCountry=this.areaOption[0]
        //     break
        //   }
        // }
      },
    },
  },
  components:{
    IndexFooter
  }
}
